{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import os\n",
    "import cv2\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.utils import shuffle\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l_samp = !ls ./dataset/*vehicles/*/*\n",
    "\n",
    "M_ClassDict = {\"non-vehicles\" : 0, \"vehicles\" : 1}\n",
    "pd_SampClass = pd.DataFrame({\n",
    "    \"Sample\" : l_samp, \n",
    "    \"Class\"   : list(map(lambda x: M_ClassDict[x], list(map(lambda x: x.split(\"/\")[2], l_samp))))\n",
    "})[['Sample', 'Class']]\n",
    "pd_SampClass_train,pd_SampClass_cv = train_test_split(pd_SampClass, test_size=0.33, random_state=42)\n",
    "pd_SampClass_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "l_imagepath = pd_SampClass_train['Sample'][0:32]\n",
    "def get_image(path, shape=None):\n",
    "    image = cv2.imread(path)\n",
    "    image = image[:,:,::-1]\n",
    "    if shape != None:\n",
    "        image = cv2.resize(image, shape)\n",
    "    return image\n",
    "\n",
    "\n",
    "l_img = []\n",
    "for imagepath in l_imagepath:\n",
    "    img = get_image(imagepath)\n",
    "    l_img.append(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(l_img[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def image_generator(pd_input, shape=(64,64), batch_size=32):\n",
    "    num_samples = pd_input.shape[0]\n",
    "    while 1:\n",
    "        pd_input_shuffle = shuffle(pd_input)\n",
    "        for offset in range(0, num_samples, batch_size):\n",
    "            l_x = []\n",
    "            l_y = []\n",
    "            for idx in range(batch_size):\n",
    "                batch_samples = pd_input_shuffle.iloc[offset:offset+batch_size]\n",
    "                try:\n",
    "                    path  = batch_samples.iloc[idx]['Sample']\n",
    "                    label = batch_samples.iloc[idx]['Class']\n",
    "                    image = get_image(path, shape)\n",
    "                    l_x.append(image)\n",
    "                    l_y.append(label)\n",
    "                except:\n",
    "                    pass\n",
    "            \n",
    "            np_x = np.array(l_x)\n",
    "            np_y = np.array(l_y)\n",
    "            print(np_x.shape)\n",
    "            yield shuffle(np_x, np_y)\n",
    "            \n",
    "g = image_generator(pd_SampClass_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b = next(g)\n",
    "plt.imshow(a[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l_imagepath = ['./dataset/vehicles/KITTI_extracted/4374.png']\n",
    "l_img = []\n",
    "for imagepath in l_imagepath:\n",
    "    img = get_image(imagepath)\n",
    "    l_img.append(img)\n",
    "\n",
    "np.random.seed(0)\n",
    "train_datagen = ImageDataGenerator(\n",
    "    rescale=1.0/255,\n",
    "    shear_range=0.2,\n",
    "    zoom_range=0.2,\n",
    "    horizontal_flip=True\n",
    ")\n",
    "train_generator = train_datagen.flow(np.array(l_img))\n",
    "\n",
    "fig = plt.figure(figsize=(7,7))\n",
    "ax = fig.add_subplot(3,3,1)\n",
    "ax.imshow(l_img[0])\n",
    "ax.set_axis_off()\n",
    "ax.set_title(\"Raw Image\")\n",
    "for i in range(8):\n",
    "    imgs = next(train_generator)\n",
    "    ax = fig.add_subplot(3,3,i+2)\n",
    "    ax.imshow(imgs[0])\n",
    "    ax.set_axis_off()\n",
    "    ax.set_title(\"Augmentation %d\" % (i+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
